package com.itmumu.miniwechat.user.controller;

import com.itmumu.miniwechat.common.constants.RedisKey;
import com.itmumu.miniwechat.common.domain.response.ApiResult;
import com.itmumu.miniwechat.common.exception.BusinessException;
import com.itmumu.miniwechat.common.utils.RedisUtils;
import com.itmumu.miniwechat.user.domain.dto.LoginInfoVo;
import com.itmumu.miniwechat.user.domain.dto.RegisterFormDto;
import com.itmumu.miniwechat.user.domain.vo.response.LoginCheckCodeVo;
import com.itmumu.miniwechat.user.domain.vo.response.UserInfoVo;
import com.itmumu.miniwechat.user.service.UserService;
import com.wf.captcha.ArithmeticCaptcha;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/account")
@Slf4j
@Validated
public class AccountController {

    @Autowired
    private UserService userService;

    @RequestMapping("/checkCode")
    public ApiResult<LoginCheckCodeVo> checkCode() {

        ArithmeticCaptcha captcha = new ArithmeticCaptcha(100, 42);
        String code = captcha.text();
        log.info("登录的验证码是：{}", code);
        String checkCodeKey = UUID.randomUUID().toString();
        RedisUtils.set(RedisKey.CHECK_CODE + checkCodeKey, code, 60, TimeUnit.SECONDS);

        String checkCodeBase64 = captcha.toBase64();
        LoginCheckCodeVo loginCheckCodeVo = new LoginCheckCodeVo();
        loginCheckCodeVo.setCheckCodeBase64(checkCodeBase64);
        loginCheckCodeVo.setCode(checkCodeKey);

        return ApiResult.success(loginCheckCodeVo);
    }



    @RequestMapping("/register")
    public ApiResult register(@RequestBody RegisterFormDto registerFormDto) {
        // 校验验证码
        String checkCode = RedisUtils.get(RedisKey.CHECK_CODE + registerFormDto.getCheckCodeKey(), String.class);

        try {
            if (!Objects.nonNull(checkCode)) {
                throw new BusinessException("验证码已过期");
            }

            if (!checkCode.equalsIgnoreCase(registerFormDto.getCheckCode())) {
                throw new BusinessException("验证码错误");
            }

            // 执行用户注册
            userService.registerUser(registerFormDto);
        } finally {
            RedisUtils.del(RedisKey.CHECK_CODE + registerFormDto.getCheckCodeKey());
        }

        return ApiResult.success().message("注册成功");
    }


    @PostMapping("/login")
    public ApiResult<UserInfoVo> login(@RequestBody @Valid LoginInfoVo loginInfoVo) {

        try {
            // 校验验证码
            String cacheCheckCode = RedisUtils.get(RedisKey.CHECK_CODE + loginInfoVo.getCheckCodeKey(), String.class);
            if (!Objects.nonNull(cacheCheckCode)) {
                throw new BusinessException("验证码已过期");
            }

            if (!cacheCheckCode.equalsIgnoreCase(loginInfoVo.getCheckCode())) {
                throw new BusinessException("验证码错误");
            }

            UserInfoVo userInfoVo = userService.login(loginInfoVo.getEmail(), loginInfoVo.getPassword());
            return ApiResult.success(userInfoVo);
        } finally {
            RedisUtils.del(RedisKey.CHECK_CODE + loginInfoVo.getCheckCodeKey());
        }
    }
}
